#!/bin/env python3

import argparse
import json
from pprint import pprint
from generate_task import build_env, askfor_info, reproduce_bug
import logging
from tools.logger import add_file_handler, logger
import random
import os

logger.setLevel(logging.DEBUG)
logfile = os.path.join("./logs", f"{random.randint(1,100000)}.log")
add_file_handler(logger, logfile)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.formatter_class = argparse.RawTextHelpFormatter
    parser.description = "A CLI interface for the DataGenerator"
    subparsers = parser.add_subparsers(
        dest="command", help="commands to run", required=True
    )

    # subcommand fetch
    reproduce = subparsers.add_parser(
        "fetch", help="fetch the testcases and vul information"
    )
    reproduce.add_argument("project_name", help="project name")
    reproduce.add_argument("project_url", help="github url for the project")

    reproduce = subparsers.add_parser(
        "info", help="print the info for special project"
    )
    reproduce.add_argument("project_name", help="project name")
    # reproduce.add_argument("project_url", help="github url for the project")


    # subcommand reproduce
    scan = subparsers.add_parser("reproduce", help="reproduce the vulnerability for the target project")
    scan.add_argument("project_name", help="project name")
    scan.add_argument("vul_name", help="vulnerability name")
    #scan.add_argument("mod", help="reproduce mod, bo=build only, a=all", choices=["bo", "a"],default="a")
    # customizing the scan target
    # eg. some distro only support some versions
    # scan.add_argument(
    #     "-t",
    #     help="specify the tag to scan, None means scan all available tags",
    #     default=None,
    #     dest="target_tags",
    #     action="append",
    # )

    # scan.add_argument(
    #     "-k", "--kernel", action="store_true", help="scan Linux kernel vulnerability"
    # )

    # scan.add_argument(
    #     "-p",
    #     "--path",
    #     dest="kpath",
    #     help="local linux source code path, required if no bzImage",
    # )



    # # subcommand info
    # info = subparsers.add_parser(
    #     "info",
    #     help="get software build and depend info, print raw info if no option is specified",
    # )
    # info.add_argument("app", help="app name")
    # info.add_argument("--raw", action="store_true", help="get raw info")
    # info.add_argument("--building", action="store_true", help="get build info")
    # info.add_argument("--dependency", action="store_true", help="get dependency info")
    # info.add_argument("--tags", action="store_true", help="get all app repo tags")

    # subcommand validate
    validate = subparsers.add_parser(
        "validate", help="validate the vulnerability and software schema"
    )
    validate.add_argument(
        "-c", "--CVE", help="the CVE id to validate", default=None, dest="CVE"
    )
    validate.add_argument(
        "-k", "--kbug", help="the kernel bug id to validate", default=None, dest="kbug"
    )
    validate.add_argument(
        "-a", "--app", help="the app name to validate", default=None, dest="app"
    )

    # parse the args
    args = parser.parse_args()
    if args.command == "fetch":
        logger.info("Start fetching...")
        try:
            build_env(args.project_name, args.project_url)
            # with open(dockerfile_dir + f"{args.CVE}", "w") as f:
            #     f.write(dockerfile)
        except FileNotFoundError as e:
            logger.error(f"Fetching error {e}")
    elif args.command == "info":
        logger.info("Get info...")
        try:
            askfor_info(args.project_name)
        except FileNotFoundError as e:
            #logger.error(e)
            logger.error(f"Info error {e}")
    elif args.command == "reproduce":
        logger.info("Start reproducing...")
        try:
            reproduce_bug(args.project_name, args.vul_name)
        except FileNotFoundError as e:
            #logger.error(e)
            logger.error(f"Reproduce error {e}")
